import {clearInterval} from "timers";

export default function useCountDownExt (endTimeText:string){
    const countdown=reactive({
        day:'00',
        hou:'00',
        min:'00',
        sec:'00',
        stoped:true,
    })
    const timerID = ref<any>(null)
    const timestamp = new Date(endTimeText.replace(/-/g,'/')).getTime()
    function updateTime() {
        let date=timestamp - (new Date().getTime())
        if (date > 0) {
            let time = parseInt(date / 1000,10);
            // 获取天、时、分、秒
            let day= parseInt(time / 86400 ,10), hou=parseInt((time % 86400) / 3600 ,10),
                min =parseInt(((time % 86400) % 3600) / 60 , 10), sec=parseInt(((time % 86400) % 3600) % 60, 10)
            countdown.day = day>9? ''+day:'0'+day
            countdown.hou = hou>9? ''+hou:'0'+hou
            countdown.min = min>9? ''+min:'0'+min
            countdown.sec = sec>9? ''+sec:'0'+sec
            countdown.stoped=false
        } else {
            //活动已结束，全部设置为'00'
            // console.log("aaa")
            countdown.day='00'
            countdown.hou='00'
            countdown.min='00'
            countdown.sec='00'
            countdown.stoped=true
        }
    }
    onMounted(()=>{
        if(timerID.value)window.clearInterval(timerID.value)
        timerID.value = setInterval(updateTime, 1000);
        updateTime();
    })
    onUnmounted(()=>{
        window.clearInterval(timerID.value)
        timerID.value=null
    })
    return {
        countdown
    }
}